//
//  CXProjectDidClickCollectionViewCell.m
//  GiftThat
//
//  Created by ChuXiang on 16/1/11.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "CXProjectDidClickCollectionViewCell.h"
@interface CXProjectDidClickCollectionViewCell ()
@property (nonatomic, strong) UIImageView *maskImage;
@property (nonatomic, strong) UIImageView *imageViews;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, retain) UIImageView *ShouCangview;
@property (nonatomic, retain) UIImageView *ShouCangViewAndLb;
@property (nonatomic, retain) UILabel *ShouCanglabel;


@property (nonatomic,strong) UIImageView *unserimageview;
@property (nonatomic,strong)   UIButton *button;
@property (nonatomic,assign) BOOL BTbool;
@property (nonatomic,assign) NSInteger number;

@property (nonatomic, strong) NSMutableArray *mutableArray;
@property (nonatomic, strong) NSMutableArray *modelArray;
@end

@implementation CXProjectDidClickCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self GetData];
        self.BTbool = YES;
        self.mutableArray = [NSMutableArray arrayWithCapacity:0];
        
        self.modelArray = [NSMutableArray arrayWithCapacity:0];
 
    }
    return self;
}

-(void)GetData{
    
    self.imageViews = [[UIImageView alloc] init];
    self.imageViews.userInteractionEnabled = YES;
    [self addSubview:self.imageViews];
    
    self.maskImage = [[UIImageView alloc] init];
    self.maskImage.image = [UIImage imageNamed:@"Mengban"];
    self.maskImage.layer.cornerRadius = 10;
    self.maskImage.clipsToBounds = YES;
    
    [self addSubview:self.maskImage];

    self.label = [[UILabel alloc] init];
    [self addSubview:self.label];
    self.label.textColor = [UIColor whiteColor];
    self.label.font = [UIFont systemFontOfSize:15];
    
    self.ShouCangViewAndLb = [[UIImageView alloc]init];
    self.ShouCangViewAndLb.userInteractionEnabled = YES;
    [self.imageViews addSubview:self.ShouCangViewAndLb];
    
    //    self.ShouCangview = [[UIImageView alloc]init];
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setBackgroundImage:[UIImage imageNamed:@"Nilaixin"] forState:UIControlStateNormal];
    
    [self.ShouCangViewAndLb addSubview:self.button];
    
    
    self.ShouCanglabel = [[UILabel alloc]init];
    
    
    [self.ShouCangViewAndLb addSubview:self.ShouCangview];
    [self.ShouCangViewAndLb addSubview:self.ShouCanglabel];
    
    self.ShouCangViewAndLb.backgroundColor = [UIColor blackColor];
    self.ShouCangViewAndLb.alpha = 0.8;
    
    
    
    [self addSubview:self.label];
    
    
    
    
}



-(void)setSelectionCellModel:(SFSelectionCellModel *)SelectionCellModel{
    if (_SelectionCellModel != SelectionCellModel) {
        _SelectionCellModel = SelectionCellModel;
        
    }
    [self.imageViews sd_setImageWithURL:[NSURL URLWithString:self.SelectionCellModel.cover_image_url]placeholderImage:[UIImage imageNamed:@"holder"]];
    self.imageViews.layer.cornerRadius = 10;
    self.imageViews.clipsToBounds = YES;
    self.label.text = self.SelectionCellModel.title;
    [self.label setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:18]];
    NSString *string = [NSString stringWithFormat:@"%@",self.SelectionCellModel.likes_count];
    self.ShouCanglabel.textAlignment = NSTextAlignmentCenter;
    self.ShouCanglabel.text = string;
    [self.ShouCanglabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:10]];
    self.ShouCanglabel.textColor = [UIColor whiteColor];
    
    [self KeyValue];

    
}

//判断收藏图片状态
-(void)KeyValue{
    SAKeyValueStore *store = [[SAKeyValueStore alloc]initDBWithName:@"SFstore.db"];
    
    NSString *tabelName = @"like";
    [store createTableWithName:tabelName];
    NSArray *array = [store getAllItemsFromTable:tabelName];
    if (array.count) {
     
        
        for (SAKeyValueItem *item in array) {
            NSString *string = [NSString stringWithString:item.itemId];
            SFSelectionCellModel *model = [[SFSelectionCellModel alloc]initWithDictionary:item.itemObject];
            //      modelArray装所有元素
            [self.modelArray addObject:model];
            //       mutableArray 装所有元素的id
            [self.mutableArray addObject:string];
//            NSLog(@"%@",self.modelArray);
            for (int i = 0; i < self.mutableArray.count; i++) {
                if ([_SelectionCellModel.NewId isEqualToString:self.mutableArray[i]]) {
                    self.BTbool = YES;
                    self.ShouCanglabel.textColor = [UIColor whiteColor];
                    self.ShouCanglabel.textAlignment = NSTextAlignmentCenter;
                    [self.ShouCanglabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:10]];
                    
                    self.ShouCanglabel.text = [NSString stringWithFormat:@"%@",[self.modelArray[i] likes_count]];
                    
                    [_button setBackgroundImage:[UIImage imageNamed:@"Redaixin"] forState:UIControlStateNormal];
//                    NSLog(@"ssss");
                    break;
                    
                }else{
                    self.BTbool = NO;
                    self.ShouCanglabel.textColor = [UIColor whiteColor];
                    self.ShouCanglabel.textAlignment = NSTextAlignmentCenter;
                    [self.ShouCanglabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:10]];
                    self.ShouCanglabel.text = [_SelectionCellModel.likes_count stringValue];
                    [_button setBackgroundImage:[UIImage imageNamed:@"Nilaixin"] forState:UIControlStateNormal];
                    

                    
                }
                
            }
            
        }
        
    }else{
        
        self.ShouCanglabel.textAlignment = NSTextAlignmentCenter;
        self.number = _SelectionCellModel.likes_count.integerValue;
        self.ShouCanglabel.text = [NSString stringWithFormat:@"%ld",(long)self.number];
        [self.ShouCanglabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:10]];
        self.ShouCanglabel.textColor = [UIColor whiteColor];
        
        [self.button setBackgroundImage:[UIImage imageNamed:@"Nilaixin"] forState:UIControlStateNormal];
        
    }
    
}



-(void)buttonDidPress:(UIButton *)sender{
    NSLog(@"1");
//    self.BTbool = !self.BTbool;
    if (self.BTbool == NO) {
        SAKeyValueStore *store = [[SAKeyValueStore alloc]initDBWithName:@"SFstore.db"];
        NSString *tableName = @"like";
        [store createTableWithName:tableName];
        self.number = _SelectionCellModel.likes_count.integerValue + 1;
        NSDictionary *dic = @{@"title":_SelectionCellModel.title,@"cover_image_url":_SelectionCellModel.cover_image_url,@"url":_SelectionCellModel.url,@"likes_count":[NSString stringWithFormat:@"%ld",(long)self.number]};
        
        [store putObject:dic withId:_SelectionCellModel.NewId intoTable:tableName];
        [store close];
        [self.button setBackgroundImage:[UIImage imageNamed:@"Redaixin"] forState:UIControlStateNormal];
        
        self.ShouCanglabel.text = [NSString stringWithFormat:@"%ld",(long)self.number];
        self.BTbool = YES;
        
    }else{
        
        SAKeyValueStore *store = [[SAKeyValueStore alloc]initDBWithName:@"SFstore.db"];
        NSString *tableName = @"like";
        [store createTableWithName:tableName];
        [store deleteObjectById:_SelectionCellModel.NewId fromTable:tableName];
        [self.button setBackgroundImage:[UIImage imageNamed:@"Nilaixin"] forState:UIControlStateNormal];
        
        
        self.number = _SelectionCellModel.likes_count.integerValue;
        self.ShouCanglabel.text = self.SelectionCellModel.likes_count.stringValue;
        //        [store clearTable:tableName];
        self.BTbool = NO;
    }
    
    
    
    
    [self.imageViews sd_setImageWithURL:[NSURL URLWithString:self.SelectionCellModel.cover_image_url]placeholderImage:[UIImage imageNamed:@"holder"]];
    self.imageViews.layer.cornerRadius = 10;
    self.imageViews.clipsToBounds = YES;
    self.label.text = self.SelectionCellModel.title;
    //      [self.label setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:15]];
    NSString *string = [NSString stringWithFormat:@"%@",self.SelectionCellModel.likes_count];
    self.ShouCanglabel.textAlignment = NSTextAlignmentCenter;
    self.ShouCanglabel.text = string;
    [self.ShouCanglabel setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:10]];
    self.ShouCanglabel.textColor = [UIColor whiteColor];
    
    
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    
    self.imageViews.frame = CGRectMake(5, 5, self.width - 10, self.height - 10);
    self.maskImage.frame = CGRectMake(5, self.height - 45, self.width - 10, 40);
    
    self.label.frame = CGRectMake(10, self.height - 40, self.width, 40);
    self.ShouCangViewAndLb.frame = CGRectMake(self.width - 60, 0, 40, 40);
    self.button.frame = CGRectMake(5, 0, 30, 25);
    self.ShouCanglabel.frame = CGRectMake(0, self.ShouCangViewAndLb.height - 15, self.ShouCangViewAndLb.width , 15);
    
    self.unserimageview.frame = CGRectMake(5, self.height - 45, self.width - 10, 40);
    
    
    
}

@end
